/// @tags:
#include <cstdio>
#include <iostream>

using namespace std;

namespace BlueQuantum {

int const N = 3e3 + 5, M = 6e3 + 5;

struct Edge {
  int v, nxt, w;
  bool typ;
} e[M << 1];

int n, m, k, e_cnt, tot, rt;
int heads[N], buk[N];
bool mp[N][N], vis[M << 1];

inline void add(int u, int v, int w, bool typ) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt, e[e_cnt].w = w,
  e[e_cnt].typ = typ;
}

void solve(int u, int fa) {
  for (int i = heads[u], v; i; i = e[i].nxt) {
    if (!vis[i] && (v = e[i].v) != fa) {
      if (e[i].typ && buk[e[i].w]) {
        buk[e[i].w]--, tot--;
        if (tot == 0) mp[rt][v] = true;
        vis[i] = true;
        solve(v, u);
        vis[i] = false;
        buk[e[i].w]++, tot++;
      }
      if (!e[i].typ) {
        buk[e[i].w]++, tot++;
        vis[i] = true;
        solve(v, u);
        vis[i] = false;
        buk[e[i].w]--, tot--;
      }
    }
  }
}

inline int main() {
  cin >> n >> m >> k;
  for (int i = 1, u, v, w; i <= m; ++i) {
    cin >> u >> v >> w;
    add(u, v, w, false), add(v, u, w, true);
  }
  for (int i = 1; i <= n; ++i) { solve(rt = i, i); }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j < i; ++j) ans += mp[i][j] | mp[j][i];
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("bracket.in", "r", stdin);
  freopen("bracket.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
